package com.example.mallproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;
import com.example.mallproduct.dao.CategoryBrandRelationDao;
import com.example.mallproduct.entity.BrandEntity;
import com.example.mallproduct.entity.CategoryBrandRelationEntity;
import com.example.mallproduct.entity.CategoryEntity;
import com.example.mallproduct.service.BrandService;
import com.example.mallproduct.service.CategoryBrandRelationService;
import com.example.mallproduct.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据分类查询品牌名
     *
     * @param catId
     * @return
     */
    @Override
    public List<CategoryBrandRelationEntity> brandsRelationList(Long catId) {
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        List<CategoryBrandRelationEntity> categoryBrandRelationEntityList = this.list(queryWrapper.eq("catelog_id", catId));
        return categoryBrandRelationEntityList;
    }

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;

    /**
     * 保存品牌与分类的关联
     *
     * @param categoryBrandRelation
     */
    @Override
    public void saveDetails(CategoryBrandRelationEntity categoryBrandRelation) {
        CategoryEntity category=categoryService.getById(categoryBrandRelation.getCatelogId());
        BrandEntity brand=brandService.getById(categoryBrandRelation.getBrandId());
        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());
        this.save(categoryBrandRelation);
    }

}